package org.shock.ss.sms.service.app.impl;

import org.shock.ss.sms.bean.app.InRecords;
import org.shock.ss.sms.bean.app.OutRecords;
import org.shock.ss.sms.commons.dao.BaseDao;
import org.shock.ss.sms.commons.db.ProxoolDbPool;
import org.shock.ss.sms.commons.db.TsDbTools;
import org.shock.ss.sms.commons.service.impl.BaseServiceImpl;
import org.shock.ss.sms.service.app.OutRecordsService;

public class OutRecordsServiceImpl extends BaseServiceImpl implements OutRecordsService {
	public OutRecordsServiceImpl(BaseDao dao) {
		super(dao);
	}

	@Override
	public void add(Object bean) throws Exception {
		OutRecords outRecords = (OutRecords) bean;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			dao.add(outRecords);
			TsDbTools.doSql("update " + InRecords.getTable() + " set isck=1 where productcode='"
					+ outRecords.getProductcode() + "'");
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}
	}

	@Override
	public Object addBean(Object bean) throws Exception {
		OutRecords outRecords = (OutRecords) bean;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			outRecords = (OutRecords) dao.addBean(bean);
			TsDbTools.doSql("update " + InRecords.getTable() + " set isck=1 where productcode='"
					+ outRecords.getProductcode() + "'");
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		} finally {
			return outRecords;
		}
	}

	@Override
	public void del(Object bean) throws Exception {
		OutRecords outRecords = (OutRecords) bean;
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			dao.del(outRecords);
			TsDbTools.doSql("update " + InRecords.getTable() + " set isck=0 where productcode='"
					+ outRecords.getProductcode() + "'");
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}
	}

	@Override
	public void del(String productcode) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			TsDbTools.doSql("delete from " + OutRecords.getTable() + " where productcode='" + productcode + "'");
			TsDbTools.doSql("update " + InRecords.getTable() + " set isck=0 where productcode='" + productcode + "'");
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}
	}

}